package com.mvcplus.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mvcplus.common.controller.BaseController;
import com.mvcplus.system.bo.ResourceBo;
import com.mvcplus.system.model.SysResource;
import com.mvcplus.system.service.ISysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by BINGO on 2016/10/28.
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("/resource")
public class ResourceController extends BaseController {

    private final ISysResourceService resourceService;

    @Autowired
    public ResourceController(ISysResourceService resourceService) {
        this.resourceService = resourceService;
    }

    @RequestMapping("/")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("system/resource");
        modelAndView.addObject("resList", resourceService.selectAllResource());
        return modelAndView;
    }

    @RequestMapping("/editPage")
    public ModelAndView editPage(ModelAndView modelAndView, Long id) {
        modelAndView.setViewName("system/resourceLayer");
        //主体对象
        SysResource sr = new SysResource();
        //父级对象
        SysResource res_p = new SysResource();
        if (id != 0) {
            sr = resourceService.selectById(id);
        }
        if (sr.getPid() != null) {
            EntityWrapper<SysResource> ew = new EntityWrapper<>();
            ew.where("id={0}", sr.getPid());
            res_p = resourceService.selectOne(ew);
        }
        modelAndView.addObject("resource", sr);
        modelAndView.addObject("res_p", res_p);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/save")
    public Object save(SysResource res) {
        res.setStatus(1);
        return resourceService.insertOrUpdateSelective(res) ? renderSuccess("保存成功") : renderError("保存失败");
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(Long id) {
        return resourceService.deleteAllById(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }

    @ResponseBody
    @RequestMapping("getSelectTree")
    public Object getSelectTree() {
        EntityWrapper ew = new EntityWrapper();
        ew.where("RESOURCE_TYPE={0}", 0);
        List<SysResource> reslist = resourceService.selectList(ew);
        return reslist;
    }


    @ResponseBody
    @RequestMapping("layerTree")
    public Object resTree(Long id) {
        List<ResourceBo> reslist = resourceService.selectResourceByRole(id);
        return reslist;
    }

    @ResponseBody
    @RequestMapping(value = "saveAuth", method = RequestMethod.POST)
    public Object saveAauth(Long roleId, @RequestParam(required = false, value = "resIds[]") Long[] resIds) {
        resourceService.saveAuth(roleId, resIds);
        return renderSuccess();
    }
}
